home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / linklist / source.lha / sorttest.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  7KB  |  344 lines

  1. /* Sort torture test */
  2.  
  3. #include    <stdio.h>
  4. #include    <string.h>
  5. #include    "list.h"
  6. #include    "time.h"
  7.  
  8. typedef struct catalog {
  9.     char        number[35];
  10.     char        title[40];
  11.     char        author[35];
  12.     int        date;
  13. } catalog;
  14.  
  15. static int    compare_author();
  16. static int    compare_title();
  17. static void    load_it();
  18. static void    print_some();
  19. static time_t    what_time();
  20. static void    prRap();
  21.  
  22. #ifdef ANSI
  23. static int
  24. compare_author(catalog *a, catalog *b)
  25. #else
  26. static int
  27. compare_author(a, b)
  28. catalog *a, *b;
  29. #endif
  30. {
  31.     int    k;
  32.  
  33.     k = strcmp(a->author, b->author);
  34.  
  35.     if (k == 0)
  36.         return(lSAME);
  37.     else if (k > 0)
  38.         return(l2LT1);
  39.     else if (k < 0)
  40.         return(l1LT2);
  41. }
  42.  
  43. #ifdef ANSI
  44. static int
  45. compare_title(catalog *a, catalog *b, int order)
  46. #else
  47. static int
  48. compare_title(a, b, order)
  49. catalog *a, *b;
  50. int order;
  51. #endif
  52. {
  53.     int    k;
  54.  
  55.     k = strcmp(a->title, b->title);
  56.  
  57.     if (k == 0)
  58.         return(lSAME);
  59.     else if (k > 0)
  60.         return(l2LT1);
  61.     else if (k < 0)
  62.         return(l1LT2);
  63. }
  64.  
  65. #ifdef ANSI
  66. static void
  67. load_it(int id, int num)
  68. #else
  69. static void
  70. load_it(id, num)
  71. int id;
  72. int num;
  73. #endif
  74. {
  75.     int    loop, size = sizeof(catalog), code;
  76.     char    c1, c2, c3, c4, c5, c6, c7, c8;
  77.     catalog    rap;
  78.  
  79.     c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';
  80.     c7--;
  81.     for (loop=1; loop<=num; loop++) {
  82.         c7++;
  83.  
  84.         if (c7>'Z') {c7 = 'A'; c6++;}
  85.         if (c6>'Z') {c6 = 'A'; c5++;}
  86.         if (c5>'Z') {c5 = 'A'; c4++;}
  87.         if (c4>'Z') {c4 = 'A'; c3++;}
  88.         if (c3>'Z') {c3 = 'A'; c2++;}
  89.         if (c2>'Z') {c2 = 'A'; c1++;}
  90.         if (c1>'Z') {c1 = 'A'; c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';}
  91.  
  92.         sprintf(rap.number,"B-90-%d", loop);
  93.         sprintf(rap.title, "Book %d", loop);
  94.         sprintf(rap.author,"%c%c%c%c%c%c%c%d",
  95.             c1, c2, c3, c4, c5, c6, c7, loop);
  96.         rap.date = 890129;
  97.         code = lInsNode(id, lLAST, &rap, size, loop);
  98.     }
  99.     /* printf("Linked List created\n"); */
  100. }
  101.  
  102. #ifdef ANSI
  103. static void
  104. print_some(int id, int num)
  105. #else
  106. static void
  107. print_some(id, num)
  108. int id;
  109. int num;
  110. #endif
  111. {
  112.     int    size = sizeof(catalog), loop;
  113.     int    code;
  114.     catalog    rap;
  115.  
  116.     code = lGetNode(id, lFIRST, &rap, size);
  117.     prRap(code, &rap);
  118.  
  119.     for (loop=2; loop<=num; loop++) {
  120.         code = lGetNode(id, lNEXT, &rap, size);
  121.         prRap(code, &rap);
  122.     }
  123. }
  124.  
  125. #ifdef ANSI
  126. static time_t what_time(void)
  127. #else
  128. static time_t what_time()
  129. #endif
  130. {
  131.     return((time_t)time(NULL));
  132. }
  133.  
  134. #ifdef ANSI
  135. static void diff_time(time_t a, time_t b)
  136. #else
  137. static void diff_time(a, b)
  138. time_t a, b;
  139. #endif
  140. {
  141.     struct tm    *tt;
  142.     char        buf1[101], buf2[101];
  143.     int        hour[2], min[2], sec[2];
  144.     time_t        x;
  145.  
  146.     tt = (struct tm *)malloc(sizeof(struct tm *) + 1);
  147.  
  148.     tt = (struct tm *)localtime(&a);
  149.     strftime(buf1, 100, "%H %M %S", tt);
  150.  
  151.     tt = (struct tm *)localtime(&b);
  152.     strftime(buf2, 100, "%H %M %S", tt);
  153.  
  154.     sscanf(buf1,"%d %d %d", &hour[0], &min[0], &sec[0]);
  155.     sscanf(buf2,"%d %d %d", &hour[1], &min[1], &sec[1]);
  156.  
  157.     printf("Hours = %d  Mins = %d  Secs = %d\n",
  158.     (hour[1] - hour[0] > 0)?(hour[1] - hour[0]) : (hour[0] - hour[1]),
  159.     (min[1] - min[0] > 0)?(min[1] - min[0]) : (min[0] - min[1]),
  160.     (sec[1] - sec[0] > 0)?(sec[1] - sec[0]) : (sec[0] - sec[1]));
  161. }
  162.  
  163. main()
  164. {
  165.     int    size = sizeof(catalog);
  166.     int    id, code;
  167.     time_t    start, finish;
  168.     catalog    rap;
  169.  
  170.     /* Quick sort */
  171.     /* start testing sorting efficiency */
  172.     id = lDef(lSINGLY, lCHAIN);
  173.     load_it(id, 3000);
  174.  
  175.     printf("Untouched list\n");
  176.     print_some(id, 10);
  177.  
  178.     printf("-- lQuickSort --\nlQuickSort DESCENDING by TITLE : ");
  179.     start = what_time();
  180.     code = lSort(id, lDESCENDING, lQUICK, compare_title);
  181.     finish = what_time();
  182.     diff_time(start, finish);
  183.     print_some(id, 10);
  184.  
  185.     printf("lQuickSort ASCENDING by TITLE : ");
  186.     start = what_time();
  187.     code = lSort(id, lASCENDING, lQUICK, compare_title);
  188.     finish = what_time();
  189.     diff_time(start, finish);
  190.     print_some(id, 10);
  191.  
  192.     printf("lQuickSort DESCENDING by AUTHOR : ");
  193.     start = what_time();
  194.     code = lSort(id, lDESCENDING, lQUICK, compare_author);
  195.     finish = what_time();
  196.     diff_time(start, finish);
  197.     print_some(id, 10);
  198.  
  199.     printf("lQuickSort ASCENDING by AUTHOR : ");
  200.     start = what_time();
  201.     code = lSort(id, lASCENDING, lQUICK, compare_author);
  202.     finish = what_time();
  203.     diff_time(start, finish);
  204.     print_some(id, 10);
  205.  
  206.     code = lDelAll();
  207.  
  208.     /* Heap sort */
  209.     /* start testing sorting efficiency */
  210.     id = lDef(lSINGLY, lCHAIN);
  211.     load_it(id, 3000);
  212.  
  213.     printf("Untouched list\n");
  214.     print_some(id, 10);
  215.  
  216.     printf(" -- lHeapSort -- \nlHeapSort DESCENDING by TITLE : ");
  217.     start = what_time();
  218.     code = lSort(id, lDESCENDING, lHEAP, compare_title);
  219.     finish = what_time();
  220.     diff_time(start, finish);
  221.     print_some(id, 10);
  222.  
  223.     printf("lHeapSort ASCENDING by TITLE : ");
  224.     start = what_time();
  225.     code = lSort(id, lASCENDING, lHEAP, compare_title);
  226.     finish = what_time();
  227.     diff_time(start, finish);
  228.     print_some(id, 10);
  229.  
  230.     printf("lHeapSort ASCENDING by AUTHOR : ");
  231.     start = what_time();
  232.     code = lSort(id, lASCENDING, lHEAP, compare_author);
  233.     finish = what_time();
  234.     diff_time(start, finish);
  235.     print_some(id, 10);
  236.  
  237.     code = lDelAll();
  238.  
  239.     /* Selection sort */
  240.     /* start testing sorting efficiency */
  241.     id = lDef(lSINGLY, lCHAIN);
  242.     load_it(id, 3000);
  243.  
  244.     printf("Untouched list\n");
  245.     print_some(id, 10);
  246.  
  247.     printf("lSelectionSort DESCENDING by TITLE : ");
  248.     start = what_time();
  249.     code = lSort(id, lDESCENDING, lSELECTION, compare_title);
  250.     finish = what_time();
  251.     diff_time(start, finish);
  252.     print_some(id, 10);
  253.  
  254.     printf("lSelectionSort ASCENDING by TITLE : ");
  255.     start = what_time();
  256.     code = lSort(id, lASCENDING, lSELECTION, compare_title);
  257.     finish = what_time();
  258.     diff_time(start, finish);
  259.     print_some(id, 10);
  260.  
  261.     printf("lSelectionSort ASCENDING by AUTHOR : ");
  262.     start = what_time();
  263.     code = lSort(id, lASCENDING, lSELECTION, compare_author);
  264.     finish = what_time();
  265.     diff_time(start, finish);
  266.     print_some(id, 10);
  267.  
  268.     code = lDelAll();
  269.  
  270.     /* insertion sort*/
  271.     /* start testing sorting efficiency */
  272.     id = lDef(lSINGLY, lCHAIN);
  273.     load_it(id, 3000);
  274.  
  275.     printf("Untouched list\n");
  276.     print_some(id, 10);
  277.  
  278.     printf("lInsertSort DESCENDING by TITLE : ");
  279.     start = what_time();
  280.     code = lSort(id, lDESCENDING, lINSERT, compare_title);
  281.     finish = what_time();
  282.     diff_time(start, finish);
  283.     print_some(id, 10);
  284.  
  285.     printf("lInsertSort ASCENDING by TITLE : ");
  286.     start = what_time();
  287.     code = lSort(id, lASCENDING, lINSERT, compare_title);
  288.     finish = what_time();
  289.     diff_time(start, finish);
  290.     print_some(id, 10);
  291.  
  292.     printf("lInsertSort ASCENDING by AUTHOR : ");
  293.     start = what_time();
  294.     code = lSort(id, lASCENDING, lINSERT, compare_author);
  295.     finish = what_time();
  296.     diff_time(start, finish);
  297.     print_some(id, 10);
  298.  
  299.     code = lDelAll();
  300.  
  301.     /* start testing sorting efficiency */
  302.     id = lDef(lSINGLY, lCHAIN);
  303.     load_it(id, 3000);
  304.  
  305.     printf("Untouched list\n");
  306.     print_some(id, 10);
  307.  
  308.     /* Bubble sort */
  309.     printf("lBubbleSort DESCENDING by TITLE : ");
  310.     start = what_time();
  311.     code = lSort(id, lDESCENDING, lBUBBLE, compare_title);
  312.     finish = what_time();
  313.     diff_time(start, finish);
  314.     print_some(id, 10);
  315.  
  316.     printf("lBubbleSort ASCENDING by TITLE : ");
  317.     start = what_time();
  318.     code = lSort(id, lASCENDING, lBUBBLE, compare_title);
  319.     finish = what_time();
  320.     diff_time(start, finish);
  321.     print_some(id, 10);
  322.  
  323.     printf("lBubbleSort ASCENDING by AUTHOR : ");
  324.     start = what_time();
  325.     code = lSort(id, lASCENDING, lBUBBLE, compare_author);
  326.     finish = what_time();
  327.     diff_time(start, finish);
  328.     print_some(id, 10);
  329.  
  330.     code = lDelAll();
  331. }
  332.  
  333. static void
  334. prRap(code, rpprt)
  335. int            code;
  336. catalog        *rpprt;
  337. {
  338.     if (code >= 0)
  339.         printf("catalog :'%s' '%s' '%s' '%d'\n", rpprt->number, rpprt->title,
  340.             rpprt->author, rpprt->date);
  341.     else
  342.         printf("Return code = %d\n", code);
  343. }
  344.